package com.htt.app.babau.sqlite;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.htt.app.babau.utils.Log;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDataBaseHelper extends SQLiteOpenHelper {
	
	private static String TAG = "MyDataBaseHelper";
	
	private static String DB_PATH = "";
	private static String DB_NAME = "babau.db"; // Database name 
	private SQLiteDatabase db;
	private Context context;
	private static final int DB_VERSION = 1;
	
	// lock db
	public static String Lock = "dblock";
	
	// field table categories
	public static final String Categories = "Categories";
	public static final String CategoriesId = "id";
	public static final String CategoriesName = "name";
	public static final String CategoriesImage = "image";
	
	// field table details
	public static final String Details = "Details";
	public static final String DetailID = "id";
	public static final String DetailCategoriesId = "categories_id";
	public static final String DetailTitle = "title";
	public static final String DetailContent = "content";
	public static final String DetailImage = "image";
	
	public MyDataBaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		if(android.os.Build.VERSION.SDK_INT >= 4.2){
	        DB_PATH = context.getApplicationInfo().dataDir + "/databases/";         
	     }
	     else
	     {
	        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
	     }
	    this.context = context; 
	}

	public void createDataBase() throws IOException 
	{ 
	    //If database not exists copy it from the assets 
	 
	    boolean mDataBaseExist = checkDataBase(); 
	    if(!mDataBaseExist) 
	    { 
	        this.getReadableDatabase(); 
	        this.close(); 
	        try  
	        { 
	            //Copy the database from assests 
	            copyDataBase(); 
	            Log.i(TAG, "createDatabase database created"); 
	        }  
	        catch (IOException e)
	        { 
	            Log.e(TAG, "ErrorCopyingDataBase"); 
	        } 
	    } 
	} 
	    //Check that the database exists here: /data/data/your package/databases/Da Name 
	    private boolean checkDataBase() 
	    { 
	    	try {
		        File dbFile = new File(DB_PATH + DB_NAME); 
		        return dbFile.exists(); 
	    	} catch (Exception e) {
	    		e.printStackTrace();
	    		return false;
	    	}
	    } 
	 
	    //Copy the database from assets 
	    private void copyDataBase() throws IOException 
	    { 
	        InputStream mInput = context.getAssets().open(DB_NAME); 
	        String outFileName = DB_PATH + DB_NAME; 
	        OutputStream mOutput = new FileOutputStream(outFileName); 
	        byte[] mBuffer = new byte[1024]; 
	        int mLength; 
	        while ((mLength = mInput.read(mBuffer))>0) 
	        { 
	            mOutput.write(mBuffer, 0, mLength); 
	        } 
	        mOutput.flush(); 
	        mOutput.close(); 
	        mInput.close(); 
	    } 
	 
	    //Open the database, so we can query it 
	    public boolean openDataBase() throws SQLException 
	    { 
	        String mPath = DB_PATH + DB_NAME; 
	        //Log.v("mPath", mPath); 
	        try {
	        	db = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
	        }catch (Exception e) {
	        	e.printStackTrace();
	        }
	        //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
	        return db != null; 
	    } 
	 
	    @Override 
	    public synchronized void close()  
	    { 
	        if(db != null) 
	            db.close(); 
	        super.close(); 
	    }

		@Override
		public void onCreate(SQLiteDatabase arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			
		} 
	 
	} 
